/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.gxarea.service;

import java.util.HashMap;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeeplus.common.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.gxarea.entity.Community;
import com.jeeplus.modules.gxarea.mapper.CommunityMapper;
import com.jeeplus.modules.gxarea.entity.Business;
import com.jeeplus.modules.gxarea.mapper.BusinessMapper;
import com.jeeplus.modules.gxarea.entity.Counter;
import com.jeeplus.modules.gxarea.mapper.CounterMapper;
import com.jeeplus.modules.gxarea.entity.CounterByType;
import com.jeeplus.modules.gxarea.mapper.CounterByTypeMapper;

/**
 * 点位Service
 * @author azj
 * @version 2024-09-26
 */
@Service
@Transactional(readOnly = true)
public class CommunityService extends CrudService<CommunityMapper, Community> {

	@Autowired
	private BusinessMapper businessMapper;
	@Autowired
	private CounterMapper counterMapper;
	@Autowired
	private CounterByTypeMapper counterByTypeMapper;
	@Autowired
	private CommunityMapper communityMapper;
	public Community get(String id) {
		Community community = super.get(id);
		community.setBusinessList(businessMapper.findList(new Business(community)));
		community.setCounterList(counterMapper.findList(new Counter(community)));
		community.setCounterByTypeList(counterByTypeMapper.findList(new CounterByType(community)));
		return community;
	}

	public List<Community> findList(Community community) {
		return super.findList(community);
	}

	public Page<Community> findPage(Page<Community> page, Community community) {
		return super.findPage(page, community);
	}

	@Transactional(readOnly = false)
	public void save(Community community) {
		super.save(community);
		for (Business business : community.getBusinessList()){
			if (business.getId() == null){
				continue;
			}
			if (Business.DEL_FLAG_NORMAL.equals(business.getDelFlag())){
				if (StringUtils.isBlank(business.getId())){
					business.setAreaid(community);
					business.preInsert();
					businessMapper.insert(business);
				}else{
					business.preUpdate();
					businessMapper.update(business);
				}
			}else{
				businessMapper.delete(business);
			}
		}
		for (Counter counter : community.getCounterList()){
			if (counter.getId() == null){
				continue;
			}
			if (Counter.DEL_FLAG_NORMAL.equals(counter.getDelFlag())){
				if (StringUtils.isBlank(counter.getId())){
					counter.setAreaid(community);
					counter.preInsert();
					counterMapper.insert(counter);
				}else{
					counter.preUpdate();
					counterMapper.update(counter);
				}
			}else{
				counterMapper.delete(counter);
			}
		}
		for (CounterByType counterByType : community.getCounterByTypeList()){
			if (counterByType.getId() == null){
				continue;
			}
			if (CounterByType.DEL_FLAG_NORMAL.equals(counterByType.getDelFlag())){
				if (StringUtils.isBlank(counterByType.getId())){
					counterByType.setAreaid(community);
					counterByType.preInsert();
					counterByTypeMapper.insert(counterByType);
				}else{
					counterByType.preUpdate();
					counterByTypeMapper.update(counterByType);
				}
			}else{
				counterByTypeMapper.delete(counterByType);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(Community community) {
		super.delete(community);
		businessMapper.delete(new Business(community));
		counterMapper.delete(new Counter(community));
		counterByTypeMapper.delete(new CounterByType(community));
	}

	public PageBean <Community> findPageList(Page<Community> page, Community community) {
		PageBean<Community> pageBean = new PageBean<>();
		PageHelper.startPage(page.getPageNo(), page.getPageSize());
		List<Community> list = communityMapper.findPageList(community);
		PageInfo <Community> pageInfo = new PageInfo<>(list);
		pageBean.setCrurentNum(page.getPageNo());
		pageBean.setPageSize(page.getPageSize());
		pageBean.setTotalNum((int) pageInfo.getTotal());
		pageBean.setTotalPageSize(pageInfo.getPages());
		pageBean.setDatas(pageInfo.getList());
		return pageBean;
	}
	/**
	 * getAreaByType
	 * 点位获取上级类别
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public List <HashMap <String,String>> getAreaByType(){
		return communityMapper.getAreaByType();
	}
	/**
	 * getDictTypeChar
	 * 获取业务前缀
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public List <HashMap <String,String>> getDictTypeChar(){
		return communityMapper.getDictTypeChar();
	}

	/**
	 * userListByCode
	 * 获取点位办理人员
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public List <HashMap <String,String>> userListByCode(String areacode){
		return communityMapper.userListByCode(areacode);
	}


	/**
	 * userListByCode
	 * 获取所有点位办理人员
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public List <HashMap <String,String>> userList(){
		return communityMapper.userList();
	}

	/**
	 * maxCheck
	 * 获取max绑定验证
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public Integer maxCheck(String max){
		return communityMapper.maxCheck(max);
	}

	/**
	 * getWinByIdList
	 *获取该区域窗口信息
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public List <HashMap <String,String>> getWinByIdList(String id){
		return communityMapper.getWinByIdList(id);
	}
	/**
	 * getTypeByIdList
	 * 获取该区域业务信息
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public List <HashMap <String,String>> getTypeByIdList(String id){
		return communityMapper.getTypeByIdList(id);
	}
}